package www.openteam.com.adapter;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.design.widget.TabLayout;
import android.support.v7.util.SortedList;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.format.Time;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import www.openteam.com.R;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.CompleteMainCustomOrderInfo;
import www.openteam.com.bean.CustomConditon;
import www.openteam.com.bean.CustomOrder;
import www.openteam.com.bean.CustomType;
import www.openteam.com.bean.DeleteInfo;
import www.openteam.com.bean.DeleteWholeInfo;
import www.openteam.com.bean.OrderApprovalitem;
import www.openteam.com.bean.PackTypeInfo;
import www.openteam.com.bean.PbProductInfo;
import www.openteam.com.bean.SomeCustomInfo;
import www.openteam.com.contract.business.adapter.EditCustomOrderContract;
import www.openteam.com.mvp.IPresenter;
import www.openteam.com.mvp.IView;
import www.openteam.com.presenter.business.EditCustomOrderInfoPresenter;
import www.openteam.com.tool.ToastUtil;

import static www.openteam.com.tool.SplitXML.splitStringFloat;
import static www.openteam.com.tool.SplitXML.splitStringInt;
import static www.openteam.com.tool.SplitXML.splitStringString;

/**
 * Created by kylin on 2016/9/26.
 */

public class CustomOrderAdapter<P extends IPresenter>
        extends RecyclerView.Adapter
        implements EditCustomOrderContract.EditCustomOrderView, IView {
    private static final String TAG = CustomOrderAdapter.class.getSimpleName();
    private LayoutInflater mLayoutInflater;
    //CustomOrder全部信息
    private SortedList<CustomOrder.DataEntity> mSortedList;

    private ArrayList<CustomConditon> customConditons = new ArrayList<CustomConditon>();
    //子订单
    private ArrayList<OrderApprovalitem> orderSubitem = new ArrayList<OrderApprovalitem>();
    private Context context;
    //
    private OrderApprovalitem orderApprovalitem;
    private View mGridView;// 子Layout要以view的形式加入到主Layout
    private LinearLayout mLinearLayout;//对应于主布局中用来添加子布局的View
    private boolean showStatue;
    private List<View> ls_childView;//用于保存动态添加进来的View
    //item的Holder
    private ArrayList<ItemViewHolder> lsvh;
    private ArrayList<LinearLayout> linearlayoutList = new ArrayList<>();
    private ArrayList<LinearLayout> uplinearlayoutList = new ArrayList<>();
    private ArrayList<LinearLayout> lowlinearlayoutList = new ArrayList<>();
    private ButtonInterface buttonInterface;
    private PbProductInfo.DataEntity dataEntity;
    private PackTypeInfo.DataEntity packType;
    private SomeCustomInfo.DataEntity customs;
    private CustomType.DataBean pmcType;
    //完全的订单信息
    private CompleteMainCustomOrderInfo completeMainCustomOrderInfo;
    //选择的子订单id
    int childID = 0;
    //选的linearLayout id
    int linearLayoutId = 0;
    private ProgressDialog mProgressDialog;
    private int year;
    private int monthOfYear;
    private int dayOfMonth;
    private String[] prodname;
    protected EditCustomOrderInfoPresenter mPresenter;
    private EditText et_order_dproductname;
    private EditText et_order_dproductno;
    private EditText et_order_technicsinf;
    private EditText et_order_paperinf;
    private EditText et_order_customno;
    private EditText et_order_stype;
    private EditText et_order_payname;
    private EditText et_order_moneyname;
    private EditText et_order_customname;
    private EditText et_order_wraptype;
    private EditText et_order_money;
    private EditText et_order_sendname;
    private EditText et_order_stype1;
    private EditText et_order_payname1;
    private IonClickListenter ionClickListenter;
    private boolean ischeck;
    private ArrayList<Boolean> ischeckList = new ArrayList<>();

    /**
     * 动态的更新数据
     *
     * @param context
     */
    public CustomOrderAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(CustomOrder.DataEntity.class, new SortedList.Callback<CustomOrder.DataEntity>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(CustomOrder.DataEntity o1, CustomOrder.DataEntity o2) {

                if (Integer.parseInt(o1.getRownumber()) < Integer.parseInt(o2.getRownumber())) {
                    return -1;
                } else if (Integer.parseInt(o1.getRownumber()) > Integer.parseInt(o2.getRownumber())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(CustomOrder.DataEntity oldItem, CustomOrder.DataEntity newItem) {
//                Log.d(TAG, "areContentsTheSame: " + 1);
                return oldItem.getRownumber().equals(newItem.getRownumber());
            }

            @Override
            public boolean areItemsTheSame(CustomOrder.DataEntity item1, CustomOrder.DataEntity item2) {

                boolean statue;
                if (item1.getId() == item2.getId() &&
                        item1.getPrice().equals(item2.getPrice()) &&
                        item1.getUserid() == item2.getUserid()) {
                    statue = true;
                } else {
                    statue = false;
                }

                return statue;

            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
        mPresenter = loadPresenter();
        initCommonData();
    }

    public interface IonClickListenter{
        void onClickModify(int postion);

        void onClickCardView(int postion,boolean isInstpect);
    }

    public void setIonClickListenter(IonClickListenter ionClickListenter) {
        this.ionClickListenter = ionClickListenter;
    }

    //  UpdateCustomOrderItemInfoView{//
    private void initCommonData() {
        if (mPresenter != null)
            mPresenter.attachView(this);
    }


    protected EditCustomOrderInfoPresenter loadPresenter() {

        return new EditCustomOrderInfoPresenter();

    }

    public void addItems(ArrayList<CustomOrder.DataEntity> list) {
        mSortedList.beginBatchedUpdates();
        for (CustomOrder.DataEntity itemModel : list) {
            mSortedList.add(itemModel);
        }
        mSortedList.endBatchedUpdates();

    }

    public void addContext(Context context) {
        this.context = context;
    }

    public void deleteItems(ArrayList<CustomOrder.DataEntity> items) {
        mSortedList.beginBatchedUpdates();
        for (CustomOrder.DataEntity item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    public void addData(PbProductInfo.DataEntity dataEntity) {
        this.dataEntity = dataEntity;

        et_order_dproductname = (EditText) linearlayoutList.get(linearLayoutId).
                getChildAt(childID).findViewById(R.id.et_order_dproductname);
        et_order_dproductno = (EditText) linearlayoutList
                .get(linearLayoutId).getChildAt(childID).
                        findViewById(R.id.et_order_dproductno);
        et_order_technicsinf = (EditText) linearlayoutList.
                get(linearLayoutId).getChildAt(childID).findViewById(R.id.et_order_technicsinf);
        et_order_paperinf = (EditText) linearlayoutList.
                get(linearLayoutId).getChildAt(childID).findViewById(R.id.et_order_paperinf);

        et_order_dproductname.setText(dataEntity.getPbName());
        et_order_dproductno.setText(dataEntity.getCode());
        et_order_technicsinf.setText(dataEntity.getTechnicsinf());
        et_order_paperinf.setText(dataEntity.getPaperInf());
    }

    public void addUpdateData(SomeCustomInfo.DataEntity dataEntity) {
        this.customs = dataEntity;
        completeMainCustomOrderInfo.setCustid(dataEntity.getCode());
        completeMainCustomOrderInfo.setCustname(dataEntity.getName());
        completeMainCustomOrderInfo.setStype(dataEntity.getType());
        completeMainCustomOrderInfo.setPaytime(dataEntity.getPaytime());
        completeMainCustomOrderInfo.setMoneytype(dataEntity.getMoneytype());
        et_order_customno = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_customno);
        et_order_stype = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_stype);
        et_order_payname = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_payname);
        et_order_moneyname = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_moneyname);
        et_order_customname = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_customname);
        et_order_customno.setText(customs.getCode());
        et_order_customname.setText(customs.getName());
        et_order_stype.setText(customs.getTypename());
        et_order_payname.setText(customs.getPayname());
        et_order_moneyname.setText(customs.getMoneyname());
    }

    public void addPackTypeData(PackTypeInfo.DataEntity dataEntity) {
        this.packType = dataEntity;

        et_order_wraptype = (EditText) linearlayoutList.get(linearLayoutId).getChildAt(childID).findViewById(R.id.et_order_wraptype);
//        Log.d(TAG, "addData: "+ et_order_dproductname.getText()+":::"+childID+"::::"+mLinearLayout.getChildCount());
        et_order_wraptype.setText(dataEntity.getName());
    }

    public void addPMCData(CustomType.DataBean dataBean, int type) {
        this.pmcType = dataBean;
        switch (type) {
            case 0:
                et_order_money = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_moneyname);
                et_order_money.setText(pmcType.getName());
                completeMainCustomOrderInfo.setPaytime(pmcType.getCode());
                break;
            case 1:
                et_order_payname1 = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_payname);
                et_order_payname1.setText(pmcType.getName());
                completeMainCustomOrderInfo.setPaytime(pmcType.getCode());
                break;
            case 2:
                et_order_stype1 = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_stype);
                et_order_stype1.setText(pmcType.getName());
                completeMainCustomOrderInfo.setPaytime(pmcType.getCode());
                break;
            case 3:
                et_order_sendname = (EditText) uplinearlayoutList.get(linearLayoutId).findViewById(R.id.et_order_sendname);
                et_order_sendname.setText(pmcType.getName());
                completeMainCustomOrderInfo.setSendtype(pmcType.getCode());
                break;
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CustomOrderAdapter.mViewHolder mTheViewHolder = new CustomOrderAdapter.mViewHolder(
                mLayoutInflater.inflate(R.layout.item_custom_order, parent, false));
        return mTheViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final mViewHolder viewHolder = (mViewHolder) holder;
        final CustomOrder.DataEntity item = mSortedList.get(position);
        viewHolder.mLinearLayout.removeAllViews();
        viewHolder.tv_order_rowno.setText(item.getRownumber());
        viewHolder.tv_order_rdsno.setText(item.getRdsno());
        viewHolder.tv_order_customno.setText(item.getCustid());
        viewHolder.tv_order_customname.setText(item.getCustname());
        viewHolder.tv_order_moneyname.setText(item.getMoneyname());
        viewHolder.tv_order_payname.setText(item.getPayname());
        viewHolder.tv_order_sendname.setText(item.getSendname());
        viewHolder.salesman.setText(item.getUserid() + "");
        viewHolder.tv_order_alreadlypay.setText(item.getAlreadlypay());
        viewHolder.tv_order_infoamout.setText(item.getOrderamount());
        viewHolder.tv_order_inputdate.setText(item.getInputdate());
        viewHolder.tv_order_takegoods.setText(item.getSendaddress());
        viewHolder.tv_order_orderdate.setText(item.getOrderdate());
        if (item.getIstax() == 1) {
            viewHolder.tv_order_istax.setText("是");
        } else {
            viewHolder.tv_order_istax.setText("否");
        }
        viewHolder.tv_order_taxrate.setText(item.getTaxrate());
        viewHolder.tv_order_invtype.setText(item.getInvtype());
        viewHolder.tv_order_stype.setText(item.getStype());
        viewHolder.tv_order_remark.setText(item.getRemark());


//
        if (item.getStatusn().equals("已审核")) {
            ischeckList.add(true);
            viewHolder.btn_modify.setVisibility(View.INVISIBLE);
            viewHolder.btn_delete.setVisibility(View.INVISIBLE);
            viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.red_shallow));
        } else  {
            ischeckList.add(false);
            viewHolder.btn_delete.setVisibility(View.VISIBLE);
            viewHolder.btn_modify.setVisibility(View.VISIBLE);
            viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.green));
        }


        viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

                final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);

                builder.setTitle("删除提示");
                builder.setMessage("是否删除'订单编号':" + item.getRdsno() + "的订单");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.deleteCustomOrderInfo(item.getId());

                        if (!showStatue) {
                            showStatue = !showStatue;
                            CardView c = (CardView) v.getParent().getParent()
                                    .getParent().getParent().getParent();
                            c.removeAllViews();
                        }
                        dialog.dismiss();
                    }
                });
                builder.show();


            }
        });

//        //新建父订单
//        viewHolder.btn_new.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                buttonInterface.onClickNewBuild();
//            }
//        });

        //

        //父订单点击跳转到子订单
        viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ionClickListenter!=null){
                    ionClickListenter.onClickCardView(position,ischeckList.get(position));
                }
            }
        });

        viewHolder.btn_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ionClickListenter!=null){
                    ionClickListenter.onClickModify(position);
                }
            }
        });




//        viewHolder.et_order_customno.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                linearLayoutId = position;
//                buttonInterface.onclickSearchCustom();
//            }
//        });
//
//
//        viewHolder.et_order_moneyname.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                linearLayoutId = position;
//                buttonInterface.onclickSearchMoneyName();
//            }
//        });
//
//        viewHolder.et_order_payname.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                linearLayoutId = position;
//                buttonInterface.onclickSearchCopay();
//            }
//        });
//        viewHolder.et_order_sendname.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                linearLayoutId = position;
//                buttonInterface.onclickSearchSendName();
//            }
//        });
//        viewHolder.et_order_inputdate.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                selectCalendar((View) v.getParent().getParent().getParent(), 0);
//            }
//        });
//
//        viewHolder.et_order_orderdate.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                selectCalendar((View) v.getParent().getParent().getParent(), 1);
//            }
//        });
    }


    /**
     * 创建子布局
     *
     * @param childView
     * @param cuss
     * @param mark
     * @param position
     */
    private void getViewInstance(final View childView, final OrderApprovalitem cuss, final int mark, final int position) {
        final ItemViewHolder vh = new ItemViewHolder();
        vh.id = childView.getId();
        Log.d(TAG, "getViewInstance: " + mark);
        initItemView(vh, childView, cuss);
        vh.btn_modify_detailed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showET(vh, cuss);
            }
        });

        vh.btn_modify_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTV(vh);
            }
        });

        vh.btn_new_detailed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGridView = View.inflate(context, R.layout.item_corder_detailed, null);
                int id = linearlayoutList.get(position).getChildCount();
                mGridView.setId(id);
                linearlayoutList.get(position).addView(mGridView, id);
                getNewViewInstance(mGridView, id, position, cuss.getId());
                orderSubitem.add(orderApprovalitem);
            }
        });
        vh.btn_delete_detailed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {


                final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);

                builder.setTitle("删除提示");
                builder.setMessage("是否删除" +
                        vh.tv_order_dproductname.getText().toString() + "订单子项");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.deleteCustomOrderItemInfo(cuss.getId(), cuss.getProdno());
                        if (!showStatue) {
                            Log.d(TAG, "onClick: " + showStatue);
                            RelativeLayout r = (RelativeLayout) v.getParent().getParent();
                            r.removeAllViews();
                            showStatue = !showStatue;

                        }
                        dialog.dismiss();
                    }
                });
                builder.show();


            }
        });

        vh.et_order_dproductname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                childID = mark;
                linearLayoutId = position;
                buttonInterface.onclickSearch(mark, position);
//                Log.d(TAG, "onClick: "+mark+":"+position );
            }
        });

        vh.et_order_wraptype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                childID = mark;
                linearLayoutId = position;
                buttonInterface.onclickSearchPackType();
            }
        });

        vh.btn_modify_ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vh.et_order_dproductno.getText().toString().isEmpty()
                        || vh.et_order_dproductprice.getText().toString().isEmpty()
                        || vh.et_order_dproductnumb.getText().toString().isEmpty()
                        || vh.et_order_dsenddate.getText().toString().isEmpty()
                        ) {
                    ToastUtil.showToast(context, "订单编号，价格，数量 ，送货日期不能为空");
                } else {
                    createProgressDialog();
                    mPresenter.updateCustomOrderItemInfo(
                            cuss.getId(),
                            vh.et_order_dproductno.getText().toString(),
                            vh.et_order_wraptype.getText().toString()
                            , vh.et_order_paperinf.getText().toString()
                            , vh.et_order_dsenddate.getText().toString(),
                            vh.et_order_technicsinf.getText().toString(),
                            vh.et_order_printcolor.getText().toString(),
                            vh.et_order_dproductnumb.getText().toString(),
                            vh.et_order_onum.getText().toString(),
                            vh.et_order_dproductprice.getText().toString(),
                            cuss.getProdno()
                    );

                }
            }
        });
        lsvh.add(vh);
        ls_childView.add(childView);
    }


    private void getNewViewInstance(final View childView, final int mark, final int position, final int oid) {
        final ItemViewHolder vh = new ItemViewHolder();
        vh.id = childView.getId();
        initItemView(vh, childView, null);
        showET(vh, null);

        vh.btn_modify_detailed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showET(vh, null);
            }
        });

        vh.btn_modify_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTV(vh);
            }
        });

        vh.btn_new_detailed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGridView = View.inflate(context, R.layout.item_corder_detailed, null);
                int id = linearlayoutList.get(position).getChildCount();
                mGridView.setId(id);
                linearlayoutList.get(position).addView(mGridView, id);
                getNewViewInstance(mGridView, id, position, oid);
                orderSubitem.add(orderApprovalitem);
                linearlayoutList.add(position, linearlayoutList.get(position));
            }
        });
        vh.btn_delete_detailed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);

                RelativeLayout r = (RelativeLayout) v.getParent().getParent();
                r.removeAllViews();
            }
        });

        vh.et_order_dproductname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                childID = mark;
                linearLayoutId = position;
                buttonInterface.onclickSearch(mark, position);
                Log.d(TAG, "onClick: " + mark + ":" + position);
            }
        });

        vh.et_order_wraptype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                childID = mark;
                linearLayoutId = position;
                buttonInterface.onclickSearchPackType();
            }
        });
        vh.btn_modify_ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (vh.et_order_dproductno.getText().toString().isEmpty()
                        || vh.et_order_dproductprice.getText().toString().isEmpty()
                        || vh.et_order_dproductnumb.getText().toString().isEmpty()
                        || vh.et_order_dsenddate.getText().toString().isEmpty()
                        ) {
                    ToastUtil.showToast(context, "订单编号，价格，数量 ，送货日期不能为空");
                } else {
                    Log.d(TAG, "onClick: " + oid);
                    createProgressDialog();
                    mPresenter.insertCustomOrderItemInfo(
                            oid,
                            vh.et_order_dproductno.getText().toString(),
                            vh.et_order_wraptype.getText().toString()
                            , vh.et_order_paperinf.getText().toString()
                            , vh.et_order_dsenddate.getText().toString(),
                            vh.et_order_technicsinf.getText().toString(),
                            vh.et_order_printcolor.getText().toString(),
                            vh.et_order_dproductnumb.getText().toString(),
                            vh.et_order_onum.getText().toString(),
                            vh.et_order_dproductprice.getText().toString()
                    );

                }


            }
        });
        lsvh.add(vh);
        ls_childView.add(childView);
    }


    private void createProgressDialog() {

        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setMessage("...正在更新,请您稍等...");
        mProgressDialog.show();
    }


    /**
     * 初始化日历时间
     */
    private void initCalendar() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        monthOfYear = calendar.get(Calendar.MONTH);
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        Time time = new Time("GMT+8");
        time.setToNow();
    }

    public void selectCalendar(final View tview, final int type) {
        initCalendar();

        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                EditText editText;
                switch (type) {
                    case 0:
                        editText = (EditText) tview.findViewById(R.id.et_order_inputdate);
                        break;
                    case 1:
                        editText = (EditText) tview.findViewById(R.id.et_order_orderdate);
                        break;
                    default:
                        editText = (EditText) tview.findViewById(R.id.et_order_inputdate);
                        break;
                }

                editText.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }
        }, year, monthOfYear, dayOfMonth);

        datePickerDialog.show();
    }


    /**
     * 显示所有TextView的方法
     *
     * @param view
     */
    public void showTV(ItemViewHolder view) {
        view.btn_modify_detailed.setVisibility(View.VISIBLE);
        view.btn_new_detailed.setVisibility(View.VISIBLE);
        view.btn_delete_detailed.setVisibility(View.VISIBLE);
//        view.tv_order_dproductno.setVisibility(View.VISIBLE);
//        view.tv_order_dproductname.setVisibility(View.VISIBLE);
//        view.tv_order_dproductprice.setVisibility(View.VISIBLE);
//        view.tv_order_paperinf.setVisibility(View.VISIBLE);
//        view.tv_order_wraptype.setVisibility(View.VISIBLE);
//        view.tv_order_printcolor.setVisibility(View.VISIBLE);
//        view.tv_order_dsenddate.setVisibility(View.VISIBLE);
//        view.tv_order_technicsinf.setVisibility(View.VISIBLE);
//        view.tv_order_onum.setVisibility(View.VISIBLE);
        view.btn_modify_ensure.setVisibility(View.GONE);
        view.btn_modify_cancel.setVisibility(View.GONE);
//        view.et_order_dproductno.setVisibility(View.GONE);
//        view.et_order_dproductname.setVisibility(View.GONE);
//        view.et_order_dproductprice.setVisibility(View.GONE);
//        view.tv_order_dproductnumb.setVisibility(View.GONE);
//        view.et_order_dproductnumb.setVisibility(View.GONE);
//        view.et_order_paperinf.setVisibility(View.GONE);
//        view.et_order_wraptype.setVisibility(View.GONE);
//        view.et_order_printcolor.setVisibility(View.GONE);
//        view.et_order_dsenddate.setVisibility(View.GONE);
//        view.et_order_technicsinf.setVisibility(View.GONE);
//        view.et_order_onum.setVisibility(View.GONE);
        view.table_h.setVisibility(View.GONE);
        view.table.setVisibility(View.VISIBLE);
    }

    public void showET(ItemViewHolder view, OrderApprovalitem cuss) {

        view.btn_modify_detailed.setVisibility(View.GONE);
        view.btn_new_detailed.setVisibility(View.GONE);
        view.btn_delete_detailed.setVisibility(View.GONE);
//        view.tv_order_dproductno.setVisibility(View.GONE);
//        view.tv_order_dproductname.setVisibility(View.GONE);
//        view.tv_order_dproductprice.setVisibility(View.GONE);
//        view.tv_order_paperinf.setVisibility(View.GONE);
//        view.tv_order_wraptype.setVisibility(View.GONE);
//        view.tv_order_printcolor.setVisibility(View.GONE);
//        view.tv_order_dsenddate.setVisibility(View.GONE);
//        view.tv_order_technicsinf.setVisibility(View.GONE);
//        view.tv_order_onum.setVisibility(View.GONE);
        view.btn_modify_ensure.setVisibility(View.VISIBLE);
        view.btn_modify_cancel.setVisibility(View.VISIBLE);
//        view.et_order_dproductno.setVisibility(View.VISIBLE);
//        view.et_order_dproductname.setVisibility(View.VISIBLE);
//        view.et_order_dproductprice.setVisibility(View.VISIBLE);
//        view.et_order_paperinf.setVisibility(View.VISIBLE);
//        view.et_order_wraptype.setVisibility(View.VISIBLE);
//        view.et_order_printcolor.setVisibility(View.VISIBLE);
//        view.et_order_dsenddate.setVisibility(View.VISIBLE);
//        view.et_order_technicsinf.setVisibility(View.VISIBLE);
//        view.et_order_onum.setVisibility(View.VISIBLE);
//        view.tv_order_dproductnumb.setVisibility(View.VISIBLE);
//        view.et_order_dproductnumb.setVisibility(View.VISIBLE);
        view.table_h.setVisibility(View.VISIBLE);
        view.table.setVisibility(View.GONE);
        if (cuss != null) {
            view.et_order_dproductno.setText(view.tv_order_dproductno.getText());
            view.et_order_dproductname.setText(view.tv_order_dproductname.getText());
            view.et_order_dproductprice.setText(cuss.getPrice() + "");
            view.et_order_dsenddate.setText(view.tv_order_dsenddate.getText());
            view.et_order_wraptype.setText(view.tv_order_wraptype.getText());
            view.et_order_paperinf.setText(view.tv_order_paperinf.getText());
            view.et_order_printcolor.setText(view.tv_order_printcolor.getText());
            view.et_order_technicsinf.setText(view.tv_order_technicsinf.getText());
            view.et_order_onum.setText(view.tv_order_onum.getText());
        }
    }


    public void initItemView(ItemViewHolder vh, View childView, OrderApprovalitem cuss) {
        vh.table_h = (TableLayout) childView.findViewById(R.id.table_h);
        vh.table = (TableLayout) childView.findViewById(R.id.table);
        vh.et_order_dproductno = (EditText) childView
                .findViewById(R.id.et_order_dproductno);
        vh.et_order_dproductname = (EditText) childView
                .findViewById(R.id.et_order_dproductname);
        vh.et_order_dproductprice = (EditText) childView
                .findViewById(R.id.et_order_dproductprice);
        vh.et_order_printcolor = (EditText) childView
                .findViewById(R.id.et_order_printcolor);
        vh.et_order_technicsinf = (EditText) childView
                .findViewById(R.id.et_order_technicsinf);
        vh.et_order_paperinf = (EditText) childView
                .findViewById(R.id.et_order_paperinf);
        vh.et_order_wraptype = (EditText) childView
                .findViewById(R.id.et_order_wraptype);
        vh.et_order_dsenddate = (EditText) childView
                .findViewById(R.id.et_order_dsenddate);
        vh.et_order_dproductnumb = (EditText) childView
                .findViewById(R.id.et_order_dproductnumb);
        vh.et_order_onum = (EditText) childView
                .findViewById(R.id.et_order_onum);
        vh.tv_order_dproductname = (TextView) mGridView.findViewById(R.id.tv_order_dproductname);
        vh.tv_order_dproductno = (TextView) mGridView.findViewById(R.id.tv_order_dproductno);
        vh.tv_order_dproductprice = (TextView) mGridView.findViewById(R.id.tv_order_dproductprice);
        vh.tv_order_paperinf = (TextView) mGridView.findViewById(R.id.tv_order_paperinf);
        vh.tv_order_wraptype = (TextView) mGridView.findViewById(R.id.tv_order_wraptype);
        vh.tv_order_printcolor = (TextView) mGridView.findViewById(R.id.tv_order_printcolor);
        vh.tv_order_technicsinf = (TextView) mGridView.findViewById(R.id.tv_order_technicsinf);
        vh.tv_order_onum = (TextView) mGridView.findViewById(R.id.tv_order_onum);
        vh.tv_order_dproductnumb = (TextView) mGridView.findViewById(R.id.tv_order_dproductnumb);
        vh.tv_order_dsenddate = (TextView) mGridView.findViewById(R.id.tv_order_dsenddate);
        vh.btn_modify_detailed = (Button) mGridView.findViewById(R.id.btn_modify_detailed);
//        vh.btn_new_detailed = (Button) mGridView.findViewById(R.id.btn_new_detailed);
        vh.btn_delete_detailed = (Button) mGridView.findViewById(R.id.btn_delete_detailed);
        vh.btn_modify_ensure = (Button) mGridView.findViewById(R.id.btn_modify_ensure);
//        vh.btn_modify_cancel = (Button) mGridView.findViewById(R.id.btn_modify_cancel);
        if (cuss != null) {
            vh.tv_order_dproductno.setText(cuss.getProdno());
            vh.tv_order_dproductname.setText(cuss.getProdname());
            vh.tv_order_dproductprice.setText(cuss.getPrice() + "\t*\t" + cuss.getPaynumb() + "\t=\t" + cuss.getPrice() * cuss.getPaynumb());
            vh.tv_order_paperinf.setText(cuss.getPaperinf());
            vh.tv_order_wraptype.setText(cuss.getWraptype());
            vh.tv_order_dsenddate.setText(cuss.getSenddate());
            vh.tv_order_printcolor.setText(cuss.getPrintcolor());
            vh.tv_order_technicsinf.setText(cuss.getTechnicsinf());
            vh.et_order_onum.setText(cuss.getOthernumb() + "");
            vh.et_order_dproductnumb.setText(cuss.getPaynumb() + "");
        }
    }


    @Override
    public void updateCOIData(ApprovalStatu insertStatus) {
        mProgressDialog.dismiss();
        this.buttonInterface.updateSucces();
        ToastUtil.showToast(context, "更新成功");
    }

    @Override
    public void hideCOIData(Throwable throwable) {
        mProgressDialog.dismiss();
        ToastUtil.showToast(context, "更新失败,产品编号不存在");
    }


    /**
     * 按钮点击事件需要的方法
     */

    public void buttonSetOnclick(CustomOrderAdapter.ButtonInterface buttonInterface) {
        this.buttonInterface = buttonInterface;
    }

    /**
     * 按钮点击事件对应的接口
     */
    public interface ButtonInterface {

        void onclickSearch(int mark, int position);

        void onClickNewBuild();

        void updateSucces();

        void onclickSearchPackType();

        void onclickSearchCustom();

        void onclickSearchCopay();

        void onclickSearchMoneyName();

        void onclickSearchSendName();
    }

    @Override
    public void updateCOMIData(ApprovalStatu updateStatue) {
        mProgressDialog.dismiss();
        this.buttonInterface.updateSucces();
        ToastUtil.showToast(context, "更新成功");
    }

    @Override
    public void hideCOMIData(Throwable throwable) {
        mProgressDialog.dismiss();
        ToastUtil.showToast(context, "更新失败,产品编号不存在");
    }

    @Override
    public void insertCOMIData(ApprovalStatu insertStatus) {

    }

    @Override
    public void insertFailData(Throwable throwable) {

    }


    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    @Override
    public void loadECOIIData(DeleteInfo deleteInfo) {
        showStatue = false;
        ToastUtil.showToast(context, "删除成功");
    }


    @Override
    public void hideECOIIData(Throwable throwable) {
        showStatue = true;
        ToastUtil.showToast(context, "删除失败");
    }

    @Override
    public void hideDCOIData(Throwable throwable) {
        showStatue = true;
        ToastUtil.showToast(context, "删除失败");
    }


    @Override
    public void loadDCOIData(DeleteWholeInfo deleteWholeInfo) {
        showStatue = false;
        ToastUtil.showToast(context, "删除成功");
    }


    public class ItemViewHolder {
        private int id = -1;
        private LinearLayout ll_box;
        private TableLayout table, table_h;
        private TextView tv_order_dproductname;
        private TextView tv_order_dproductno;
        private TextView tv_order_dproductprice, tv_order_printcolor, tv_order_technicsinf, tv_order_paperinf, tv_order_wraptype, tv_order_dsenddate, tv_order_onum, tv_order_dproductnumb;
        EditText et_order_dproductname, et_order_dproductno,
                et_order_dproductprice, et_order_printcolor,
                et_order_technicsinf, et_order_paperinf, et_order_wraptype, et_order_dsenddate, et_order_onum, et_order_dproductnumb;

        Button btn_modify_detailed, btn_new_detailed, btn_delete_detailed,
                btn_modify_ensure, btn_modify_cancel;


    }


    static class mViewHolder extends RecyclerView.ViewHolder {

        TextView tv_order_rowno, tv_order_rdsno, tv_order_customno,
                tv_order_customname, tv_order_moneyname,
                tv_order_payname, tv_order_sendname, salesman,
                tv_order_alreadlypay, tv_order_infoamout,
                tv_order_inputdate,
                tv_order_orderdate, tv_order_takegoods, tv_order_istax,
                tv_order_taxrate, tv_order_invtype, tv_order_stype, tv_order_remark, tv_order_dproductno, tv_order_dproductprice, tv_order_dproductname;
        Button  btn_modify, btn_delete, btn_modify_main_ensure, btn_modify_main_cancel;
        CardView cardView;
        LinearLayout mLinearLayout, ll_fold, ll_fold_et, ll_main_tv, ll_main_et, box;
        private boolean arrow_statue = true;
        EditText et_order_rowno, et_order_rdsno, et_order_customno,
                et_order_customname, et_order_moneyname,
                et_order_payname, et_order_sendname, et_salesman,
                et_order_alreadlypay, et_order_infoamout,
                et_order_inputdate,
                et_order_orderdate, et_order_takegoods,
                et_order_taxrate, et_order_invtype, et_order_stype, et_order_remark, et_order_dproductno, et_order_dproductprice, et_order_dproductname;
        Spinner sp_order_istax;
        TableLayout table, table_h;

        public mViewHolder(final View itemView) {
            super(itemView);
            tv_order_rowno = (TextView) itemView.findViewById(R.id.tv_order_rowno);
            tv_order_rdsno = (TextView) itemView.findViewById(R.id.tv_order_rdsno);
            tv_order_customno = (TextView) itemView.findViewById(R.id.tv_order_customno);
            tv_order_customname = (TextView) itemView.findViewById(R.id.tv_order_customname);
            tv_order_moneyname = (TextView) itemView.findViewById(R.id.tv_order_moneyname);
            tv_order_payname = (TextView) itemView.findViewById(R.id.tv_order_payname);
            tv_order_sendname = (TextView) itemView.findViewById(R.id.tv_order_sendname);
            salesman = (TextView) itemView.findViewById(R.id.salesman);
            tv_order_alreadlypay = (TextView) itemView.findViewById(R.id.tv_order_alreadlypay);
            tv_order_infoamout = (TextView) itemView.findViewById(R.id.tv_order_infoamout);
            tv_order_inputdate = (TextView) itemView.findViewById(R.id.tv_order_inputdate);
            tv_order_orderdate = (TextView) itemView.findViewById(R.id.tv_order_orderdate);
            tv_order_takegoods = (TextView) itemView.findViewById(R.id.tv_order_takegoods);
            tv_order_istax = (TextView) itemView.findViewById(R.id.tv_order_istax);
            tv_order_taxrate = (TextView) itemView.findViewById(R.id.tv_order_taxrate);
            tv_order_invtype = (TextView) itemView.findViewById(R.id.tv_order_invtype);
            tv_order_stype = (TextView) itemView.findViewById(R.id.tv_order_stype);
            tv_order_remark = (TextView) itemView.findViewById(R.id.tv_order_remark);
            cardView = (CardView) itemView.findViewById(R.id.c_card);
            mLinearLayout = (LinearLayout) itemView.findViewById(R.id.box);
            ll_fold = (LinearLayout) itemView.findViewById(R.id.ll_fold);
            btn_modify = (Button) itemView.findViewById(R.id.btn_modify);
            btn_delete = (Button) itemView.findViewById(R.id.btn_delete);
            ll_main_tv = (LinearLayout) itemView.findViewById(R.id.ll_main_tv);
            ll_fold_et = (LinearLayout) itemView.findViewById(R.id.ll_fold_et);
            ll_main_et = (LinearLayout) itemView.findViewById(R.id.ll_main_et);
            et_order_rdsno = (EditText) itemView.findViewById(R.id.et_order_rdsno);
            et_order_customno = (EditText) itemView.findViewById(R.id.et_order_customno);
            et_order_customname = (EditText) itemView.findViewById(R.id.et_order_customname);
            et_order_moneyname = (EditText) itemView.findViewById(R.id.et_order_moneyname);
            et_order_payname = (EditText) itemView.findViewById(R.id.et_order_payname);
            et_order_sendname = (EditText) itemView.findViewById(R.id.et_order_sendname);
            et_salesman = (EditText) itemView.findViewById(R.id.et_salesman);
            et_order_alreadlypay = (EditText) itemView.findViewById(R.id.et_order_alreadlypay);
            et_order_infoamout = (EditText) itemView.findViewById(R.id.et_order_infoamout);
            et_order_inputdate = (EditText) itemView.findViewById(R.id.et_order_inputdate);
            et_order_orderdate = (EditText) itemView.findViewById(R.id.et_order_orderdate);
            et_order_takegoods = (EditText) itemView.findViewById(R.id.et_order_takegoods);
            sp_order_istax = (Spinner) itemView.findViewById(R.id.sp_order_istax);
            et_order_taxrate = (EditText) itemView.findViewById(R.id.et_order_taxrate);
            et_order_invtype = (EditText) itemView.findViewById(R.id.et_order_invtype);
            et_order_stype = (EditText) itemView.findViewById(R.id.et_order_stype);
            et_order_remark = (EditText) itemView.findViewById(R.id.et_order_remark);

            btn_modify_main_ensure = (Button) itemView.findViewById(R.id.btn_modify_main_ensure);
            btn_modify_main_cancel = (Button) itemView.findViewById(R.id.btn_modify_main_cancel);

            table = (TableLayout) itemView.findViewById(R.id.table);
            table_h = (TableLayout) itemView.findViewById(R.id.table_h);
        }


    }


}
